﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using Timed;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using Iso;
using Iso.Common;

namespace Game.Plugin
{
    public class DialogManager : ICustomPlugin
    {
        public Queue<Dialog> dialogQueue;
        bool pressed;

        public DialogManager()
        {
            dialogQueue = new Queue<Dialog>();
            pressed = false;
        }

        public void Add(string title, Texture image, string text, string onClose)
        {
            dialogQueue.Enqueue(new Dialog(new Recti(50, 550, 974, 768), text, title, image, onClose));
        }

        public void Add(Dialog dialog)
        {
            dialogQueue.Enqueue(dialog);
        }

        public void Load()
        {

        }

        public void Unload()
        {

        }

        public void OnEvent(Event evnt)
        {

        }

        public void OnUpdate()
        {
            if (Keyboard.IsKeyDown(KeyCode.Return))
            {
                if (dialogQueue.Count != 0 && !pressed)
                    dialogQueue.Dequeue().Remove();
                pressed = true;
            }
            else
                pressed = false;
        }

        public void OnDraw()
        {
            if (dialogQueue.Count != 0)
                dialogQueue.Peek().Draw();
        }

        public PluginDrawOrder GetDrawOrder()
        {
            return PluginDrawOrder.AfterObjects;
        }
    }

    public class Dialog
    {
        public Recti position;
        public string message, title;
        public Texture picture;
        public string onClose;

        Recti imgRect, titleRect, textRect;
        Texture textTexture, titleTexture;
        const int padding = 30, titleHeight = 50;

        public Dialog(Recti position, string message, string title, Texture picture, string onClose)
        {
            this.position = position;
            this.message = message;
            this.message = title;
            this.picture = picture;
            this.onClose = onClose;

            imgRect = new Recti(position.UpperLeftCorner.X + padding, position.UpperLeftCorner.Y + padding - 110,
                position.UpperLeftCorner.X + padding + picture.Size.Width, position.LowerRightCorner.Y - padding - 110);
            titleRect = new Recti(imgRect.LowerRightCorner.X + padding, position.UpperLeftCorner.Y + padding,
                position.LowerRightCorner.X - padding, position.UpperLeftCorner.Y + padding + titleHeight);
            textRect = new Recti(position.UpperLeftCorner.X + padding + padding, titleRect.LowerRightCorner.Y + padding,
                position.LowerRightCorner.X - padding, position.LowerRightCorner.Y - padding);

            textTexture = message.RenderText(textRect.Width, textRect.Height,
                new System.Drawing.Font("Helvetica", 15), new System.Drawing.SolidBrush(System.Drawing.Color.Black));
            titleTexture = title.RenderText(titleRect.Width, titleRect.Height,
                new System.Drawing.Font("Helvetica", 20, System.Drawing.FontStyle.Bold),
                new System.Drawing.SolidBrush(System.Drawing.Color.DarkRed));
        }

        public virtual void Draw()
        {
            position.DrawNinePatch();
            Engine.device.VideoDriver.Draw2DImage(picture, imgRect, new Recti(0, 0, picture.Size.Width, picture.Size.Height), null, null, true);
            Engine.device.VideoDriver.Draw2DImage(textTexture, textRect, new Recti(0, 0, textTexture.Size.Width, textTexture.Size.Height), null, null, true);
            Engine.device.VideoDriver.Draw2DImage(titleTexture, titleRect, new Recti(0, 0, titleTexture.Size.Width, titleTexture.Size.Height), null, null, true);
        }

        public virtual void Remove()
        {
            Engine.device.VideoDriver.RemoveTexture(textTexture);
            Engine.device.VideoDriver.RemoveTexture(titleTexture);
            if (onClose != "")
                Engine.lua.GetFunction(onClose).Call();
        }
    }
}
